<template>
  <el-card class="box-card" style='margin: 10px 0'>
      <div slot="header" class="clearfix">
        <!-- 头部左侧 -->
        <el-tabs class='tab' v-model="activeName">
          <!-- v-model="activeName" @tab-click="handleClick" -->
          <el-tab-pane label="销售额" name="sale"></el-tab-pane>
          <el-tab-pane label="访问量" name="visit"></el-tab-pane>
        </el-tabs>
        <!-- 头部右侧 -->
        <div class='right'>
          <span @click='setDay'>今日</span>
          <span @click='setWeek'>本周</span>
          <span @click='setMonth'>本月</span>
          <span @click='setYear'>本年</span>
          <!-- 日历 -->
          <el-date-picker
            v-model='date'
            value-format="yyyy-MM-dd"
            class='date'
            size='mini'
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </div>
      </div>
      <!-- 内容区域 -->
      <div>
        <el-row :gutter='10'>
          <!-- 内容左侧 -->
          <el-col :span="18">
            <div class="charts" ref='charts'></div>
          </el-col>
          <!-- 内容右侧 -->
          <el-col :span="6">
            <div class="content-right">
              <h3>门店{{title}}排名</h3>
              <ul>
                <li>
                  <span class='rindex'>1</span>
                  <span class='rname'>肯德基</span>
                  <span class='rvalue'>123454</span>
                </li>
                <li>
                  <span class='rindex'>2</span>
                  <span class='rname'>肯德基</span>
                  <span class='rvalue'>12254</span>
                </li>
                <li>
                  <span class='rindex'>3</span>
                  <span class='rname'>肯德基</span>
                  <span class='rvalue'>1254</span>
                </li>
                <li>
                  <span class='rindex'>4</span>
                  <span class='rname'>肯德基</span>
                  <span class='rvalue'>125224</span>
                </li>
                <li>
                  <span class='rindex'>5</span>
                  <span class='rname'>肯德基</span>
                  <span class='rvalue'>12544</span>
                </li>
                <li>
                  <span class='rindex'>6</span>
                  <span class='rname'>肯德基</span>
                  <span class='rvalue'>125224</span>
                </li>
                <li>
                  <span class='rindex'>7</span>
                  <span class='rname'>肯德基</span>
                  <span class='rvalue'>144254</span>
                </li>
              </ul>
            </div>
          </el-col>
        </el-row>
      </div>
    </el-card>
</template>

<script>
import * as echarts from 'echarts'
import dayjs from 'dayjs'
// 使用请求来的mock数据
import {mapState} from 'vuex'

export default {
  name: 'Sale',
  data () {
    return {
      activeName: 'sale',
      mycharts: null,
      date: [],// 收集日历数据
    }
  },
  mounted () {
    this.mycharts = echarts.init (this.$refs.charts)
    this.mycharts.setOption ({
      title: {
        text: this.title + '趋势'
      },
      tooltip: {
        trigger: 'axis',
        axisPointer: {
          type: 'shadow'
        }
      },
      grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
      },
      xAxis: [
        {
          type: 'category',
          data: [],
          axisTick: {
            alignWithLabel: true
          }
        }
      ],
      yAxis: [
        {
          type: 'value'
        }
      ],
      series: [
        {
          name: 'Direct',
          type: 'bar',
          barWidth: '60%',
          data: [],
          color:'yellowgreen'
        }
      ]
    })
  },
  computed: {
    // 计算属性 标题
    title () {
      return this.activeName == 'sale'? '销售额': '访问量' // 如有警告报错 改写一下
    },
    // title: {
    //   get () {
    //     return this.activeName == 'sale'? '销售额': '访问量'
    //   },
    //   set (newValue) {
    //     return newValue
    //   }
    // },
    ...mapState ({
      listState: state => state.home.list
    })
  },
  watch: {
    // 监听变化 动态切换表格标题
    title () {
      // 修改图表title
      // console.log('切换图标')
      this.mycharts.setOption ({
        title: {
          text: this.title 
        },
      // 动态切换数据 通过监听属性
        xAxis: {
          data: this.title == '销售额'? this.listState.orderFullYearAxis:this.listState.userFullYearAxis
        },
        series: [
        {
          name: 'Direct',
          type: 'bar',
          barWidth: '60%',
          data: this.title == '销售额'? this.listState.orderFullYear:this.listState.userFullYear,
          color:'yellowgreen'
        }
        ]
      })
    },
    // 初始没有图表数据，监听服务器回来的对象，有数据就重置
    listState () {
      this.mycharts.setOption ({
      title: {
        text: this.title + '趋势'
      },
      tooltip: {
        trigger: 'axis',
        axisPointer: {
          type: 'shadow'
        }
      },
      grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
      },
      xAxis: [
        {
          type: 'category',
          data: this.listState.orderFullYearAxis,
          axisTick: {
            alignWithLabel: true
          }
        }
      ],
      yAxis: [
        {
          type: 'value'
        }
      ],
      series: [
        {
          name: 'Direct',
          type: 'bar',
          barWidth: '60%',
          data: this.listState.orderFullYear,
          color:'yellowgreen'
        }
      ]
    })
    }
  },
  methods: {
    // 本周本年本月本日
    setDay () {
      const start = dayjs().day(1).format ('YYYY-MM-DD')
      const end = dayjs().day(1).format ('YYYY-MM-DD')
      this.date = [start, end]
    },
    setWeek () {
      const start = dayjs().day(1).format ('YYYY-MM-DD')
      const end = dayjs().day(7).format ('YYYY-MM-DD')
      this.date = [start, end]
    },
    setMonth () {
      const start = dayjs().startOf('month').format ('YYYY-MM-DD')
      const end = dayjs().endOf('month').format ('YYYY-MM-DD')
      this.date = [start, end]
    },
    setYear () {
      const start = dayjs().startOf('year').format ('YYYY-MM-DD')
      const end = dayjs().endOf('year').format ('YYYY-MM-DD')
      this.date = [start, end]
    }
  }

}
</script>

<style scoped>
.clearfix {
  display: flex;
  justify-content: space-between;
}
.tab {
  width: 100%;
}
.right {
  position: absolute;
  right: 15px
}
.right span {
  margin: 0 10px;
  font-size: 14px;
}
.date {
  width: 250px;
  margin: 0 20px;
}
.charts {
  width: 100%;
  height: 300px;
}
ul {
  list-style: none;
  width: 100%;
  height: 300px;
  padding: 0;
}
ul li {
  height: 8%;
  margin: 10px 0;
}
.rindex {
  width: 20px;
  height: 20px;
  float: left;
  border-radius: 50%;
  background: black;
  color: white;
  text-align: center;
}
.rvalue {
  float: right;
}
.rname {
  margin-left: 50px;
}
</style>
